<template>
  <div class="home">
    <div class="word" @click="openToolbar">{{ wordList[index]?.word || "稍等..." }}</div>
    <div class="desc">
      <Marquee :text="wordList[index]?.explain || '加载中...'" @next="next"></Marquee>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue"
import { useInitEvent } from "../../hooks/initEvent";
import { useEvent } from "../../hooks/useEvent"
import Marquee from "../../components/Marquee/index.vue"
import { getWordList } from "../../api/index"

useInitEvent()
const { openToolbar } = useEvent()

const wordList = ref<WordItem[]>([])
const index = ref(0)
const totalPage = ref(0) // 当前书本最大页码
const params = reactive({ bookId: 1315, page: 1, limit: 100 }) // 获取列表的参数

const getList = async () => {
  const r = await getWordList(params)
  totalPage.value = r.data.totalPage
  r.data.list && wordList.value.push(...r.data.list)
}

const next = () => {
  index.value += 1
  // 当还剩下10个就再次拉取
  if (wordList.value.length - index.value <= 10 && totalPage.value < params.page) {
    params.page += 1
    getList()
  }
  // 如果轮播到最后一个 重置
  if (!wordList.value[index.value]) index.value = 0
}

onMounted(() => {
  getList()
})

</script>

<style lang="less" scoped>
.home {
  width: 100vw;
  height: 100vh;
  // -webkit-app-region: drag;
  overflow: hidden;
  padding: 2px 4px;
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: 500;
  box-sizing: border-box;
  .word {
    display: flex;
    align-items: center;
    justify-content: center;
    background: #282C34;
    padding: 4px 6px;
    border-radius: 5px;
    color: #fff;
    margin-right: 4px;
    cursor: pointer;
  }
  .desc {
    flex: 1;
    font-size: 14px;
    font-weight: normal;
    color: #fff;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    -webkit-app-region: drag;
    // cursor: crosshair;
  }
}
</style>